<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Template {

	private $CI;
	private $vars;
	private $view_path;
	private $theme;
	//private $template;
	private $theme_path;
	private $theme_view_path;
	private $module;
	private $module_view_path;
	private $assets_path;
	
	function  __construct()
	{
		$this->CI =& get_instance();
		$this->CI->config->load('template');
		
		$this->view_path = APPPATH;
		$this->template = $this->CI->config->item('template', 'template');
		$this->vars = $this->CI->config->item('vars', 'template');
		
		$this->vars['css'] = '';
		$this->vars['js'] = '';

		$this->module = $this->CI->router->fetch_module();

		$this->assets_path = APPPATH.'modules/'.$this->module.'/assets/';

		$this->set_theme($this->CI->config->item('theme', 'template'));

		$this->theme_path = APPPATH.'themes/'.$this->theme.'/';
		$this->theme_view_path = APPPATH.'themes/'.$this->theme.'/templates/'.$this->module.'/';
	}
	
	// Sets the target theme
	public function set_theme($theme)
	{
		$this->theme = $theme;
		$this->vars['theme_path'] = APPPATH.'themes/'.$this->theme;
		return $this;
	}

	// Sets the target template
	/*public function set_template($template)
	{
		$this->template = $template;
	}*/

	// Assign data to a template variable
	public function assign($vars = array(), $trigger_render = FALSE)
	{
		// assign template variables
		foreach($vars as $name => $value)
		{
			if (is_array($value))
			{
				if (isset($value['view']))
				{
					$location = $this->view_location($value['view']);

					if ($location == 'theme')
					{
						// load view from the theme
						$this->vars[$name] = $this->CI->load->view($value['view'], $value['data'], TRUE, $this->theme_view_path);
					}
					else
					{
						// load view from the module or application
						$this->vars[$name] = $this->CI->load->view($value['view'], $value['data'], TRUE);
					}
				}
				else
				{
					$this->vars[$name] = $value['data'];
				}
			}
			else
			{
				$this->vars[$name] = $value;
			}
		}

		// optionally render the main template
		if ($trigger_render)
		{
			$this->render();
		}

		return $this;
	}

	/*private function theme_path()
	{
		return APPPATH.'themes/'.$this->theme;
	}*/

	private function view_location($view)
	{
		$location = '';

		if ($this->is_loadable($this->theme_view_path.$view.'.php'))
		{
			// the view is in the theme folder
			$location = 'theme';
		}
		else
		{
			// the view is in module or application view folder
			$location = 'default';
		}

		return $location;
	}

	// Outputs data directly to the client
	function display($partial, $data)
	{
		$view = $this->get_view($partial);
		$this->CI->load->view($view, $data);
	}

	// Renders the template with definitive data
	public function render()
	{
		return $this->CI->load->view('index', $this->vars, FALSE, $this->theme_path);
	}

	private function is_loadable($view)
	{
		return get_file_info($view, 'readable');
	}

	public function add_assets($files = array())
	{
		foreach ($files as $type => $names)
		{
			foreach ($names as $name)
			{
				$path = $this->assets_path.$type.'/'.$name.'.'.$type;

				if ($type == 'css')
				{
					$this->vars['css'] .= '<link rel="stylesheet" type="text/css" media="all" href="'.$path.'" />'." \n";
				}
				else if ($type == 'js')
				{
					$this->vars['js'] .= '<script type="text/javascript" src="'.$path.'"></script>'." \n";
				}
			}
		}

		return $this;
	}
}

/* End of file Template.php */
/* Location: ./system/application/libraries/Template.php */